﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using ScrumTable.Common.Logging;
using ScrumTable.Common.Properties;

#endregion

namespace ScrumTable.Common.Threading
{
    /// <summary>
    /// Represents a synchronous data dispatcher. The actions will be
    /// executed immediately in the current thread context.
    /// </summary>
    public class SyncDataDispatcher : IDataSyncDispatcher
    {
        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Each call, which may be fired in an asynchronous context, will
        /// be synchronized using this dispatcher call.
        /// </summary>
        /// <param name="action">Specifies the action to execute in the main message loop.</param>
        public void BeginInvoke(Action action)
        {
            try
            {
                action();
            }
            catch (Exception e)
            {
                Logger<SyncDataDispatcher>.Error(Resources.ExcSyncDispError, e);
            }
        }

        /// <summary>
        /// Each call, which may be fired in an asynchronous context, will
        /// be synchronized using this dispatcher call. The callee continues its
        /// work without waiting 'till the call is complete.
        /// </summary>
        /// <param name="action">Specifies the action to execute in the main message loop.</param>
        public void Invoke(Action action)
        {
            try
            {
                action();
            }
            catch (Exception e)
            {
                Logger<SyncDataDispatcher>.Error(Resources.ExcSyncDispError, e);
            }
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}